////////////////////////////////////////
//	File Header
//	Author: Stephen Ringer | stephen.ringer@gmail.com
//	File: SAGAKeyboard.cpp
//
////////////////////////////////////////

////////////////////////////////////////
//	File Includes
#include <windows.h>       // type definitions
#include "SAGADI.h"         // class definition
//
////////////////////////////////////////

////////////////////////////////////////
//	External Global Variables
extern bool g_bLF;
//
////////////////////////////////////////

////////////////////////////////////////
//	Function Name: SAGAKeyboard
//	Purpose:
//		Constructor
SAGAKeyboard::SAGAKeyboard(LPDIRECTINPUT8 pDI, HWND hWnd, FILE* pLog) 
{ Create(pDI, hWnd, pLog); }
//
////////////////////////////////////////

////////////////////////////////////////
//	Function Name: SAGAKeyboard
//	Purpose:
//		Destructor
SAGAKeyboard::~SAGAKeyboard()
{ Log("releasing keyboard"); Release(); }
//
////////////////////////////////////////

////////////////////////////////////////
//	Function Name: Init
//	Purpose:
//		Initializes the keyboard device.
HRESULT SAGAKeyboard::Init() 
{
	Log("crancking up keyboard");
	if (FAILED(CrankUp(GUID_SysKeyboard, &c_dfDIKeyboard))) return SAGA_FAIL;

	// clear out the structs
	memset(m_Keys, 0, sizeof(m_Keys));
	memset(m_KeysOld, 0, sizeof(m_KeysOld));

	// acquire the device to make it work
	m_pDevice->Acquire();
	Log("keyboard online");
	return SAGA_OK;
} 
//
////////////////////////////////////////

////////////////////////////////////////
//	Function Name: Update
//	Purpose:
//		Update the keyboard device.
HRESULT SAGAKeyboard::Update() 
{
	// copy keyboard state from last frame
	memcpy(m_KeysOld, m_Keys, sizeof(m_Keys));

	// try to get the data from the keyboard
	if (FAILED(GetData(IDV_KEYBOARD, &m_Keys[0], NULL))) 
	{
		Log("error: GetData(Keyboard) failed");
		return SAGA_FAIL;
	}
	return SAGA_OK;
} 
//
////////////////////////////////////////

////////////////////////////////////////
//	Function Name: IsPressed
//	Purpose:
//		Check if the given key is pressed. Use WinAPI VK_ defines here.
bool SAGAKeyboard::IsPressed(UINT nID) 
{
	// translate virtual code to scan code
	if (m_Keys[nID] & 0x80) return true;
	return false;
} 
//
////////////////////////////////////////

////////////////////////////////////////
//	Function Name: IsReleased
//	Purpose:
//		Check if the given key is released. Use WinAPI VK_ defines here.
bool SAGAKeyboard::IsReleased(UINT nID) 
{
	// translate virtual code to scan code
	if ( (m_KeysOld[nID]&0x80) && !(m_Keys[nID]&0x80) ) return true;
	return false;
} 
//
////////////////////////////////////////